package com.zodiac.alioth.web.config;

import com.zodiac.alioth.web.interceptor.LoggerInterceptor;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author zodiac
 */
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"com.zodiac.alioth.web", "com.zodiac.alioth.service", "com.zodiac.alioth.common"})
@EnableAutoConfiguration
public class AppWebConfig implements WebMvcConfigurer {

    @Resource
    private LoggerInterceptor loggerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loggerInterceptor).addPathPatterns("/**");
    }

    /**
     * 配置上传文件大小的配置
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 单个数据大小
        factory.setMaxFileSize(DataSize.of(1, DataUnit.GIGABYTES));
        // 总上传数据大小
        factory.setMaxRequestSize(DataSize.of(1, DataUnit.GIGABYTES));
        return factory.createMultipartConfig();
    }

}
